package com.cdie.myschool.service.impl;

import com.cdie.myschool.common.R;
import com.cdie.myschool.domain.dto.TeachingInformationDTO;
import com.cdie.myschool.domain.entity.*;
import com.cdie.myschool.domain.vo.TeachingInformationVO;
import com.cdie.myschool.mapper.TeacherAwardsMapper.TeacherAwardsMapper;
import com.cdie.myschool.mapper.TeacherTimeTableMapper.TeacherTimeTableMapper;
import com.cdie.myschool.mapper.TeacherTimeTableMapper.TeacherTimeTableOldMapper;
import com.cdie.myschool.service.TeacherTimeTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 雷金臣199000347
 * @date 2022/10/8 - 16:52
 */
@Service
public class TeacherTimeTableServiceImpl implements TeacherTimeTableService {
    @Autowired
    private TeacherTimeTableMapper teacherTimeTableMapper;
    @Autowired
    private TeacherTimeTableOldMapper teacherTimeTableOldMapper;
    @Autowired
    private TeacherAwardsMapper teacherAwardsMapper;

    @Override
    public R<TimetableOld> TimetableOld() {
        List<TimetableOld> timetableOldList=teacherTimeTableOldMapper.TimetableOld();
        for (int i = 0; i <timetableOldList.size() ; i++) {
//            if(timetableOldList.get(i).getKcbm().equals("")||timetableOldList.get(i).getXzbjbm().equals("")){
//                continue;
//            }
//
//            System.out.println("测试1："+timetableOldList.get(i).getXzbjbm()==null);
//            System.out.println("测试2："+timetableOldList.get(i).getXzbjbm().equals(""));
//            System.out.println("测试3："+timetableOldList.get(i).getXzbjbm());
//            System.out.println("测试4："+timetableOldList.get(i).getKcbm());

//            timetableOldList.get(i).getXn();//学年
//            timetableOldList.get(i).getXq();//学期
            //学年学期id
            long semesterId=teacherTimeTableMapper.getSemesterIdInCalendar(timetableOldList.get(i).getXn(),timetableOldList.get(i).getXq());
            //换成学年学期id
//            timetableOldList.get(i).getKcbm();//课程编码
//            timetableOldList.get(i).getSkjsgh();//教师工号
//            timetableOldList.get(i).getXzbjbm();//行政班级编码
            Timetable timetable=new Timetable();
            timetable.setSemesterId(semesterId);
            timetable.setCourseId(timetableOldList.get(i).getKcbm());
            timetable.setTeacherId(timetableOldList.get(i).getSkjsgh());
            timetable.setClassesId(timetableOldList.get(i).getXzbjbm());
            timetable.setTimestamp(new Date());
            int result=teacherTimeTableMapper.InsertTeacherTimeTable(timetable);
            if(result!=1){
                return R.Failed("awards表插入失败！");
            }
        }
        return R.Success("timetable表插入成功！");
    }

    @Override
    public R<TeachingInformationVO> getTeachingInfo(TeachingInformationDTO teachingInformationDTO) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TeachingInformationVO teachingInformationVO=new TeachingInformationVO();
        Date date=new Date();//
        String pattern = "yyyy-MM-dd HH:mm:ss";
        String pattern2 ="2018-09-03 16:00:00";//2018-09-03 16:00:00同年1学期  2019-01-17 16:00:00  同年2学期  上一年2学期2018-07-05 16:00:00
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern2);
        String dateString=simpleDateFormat.format(date);
        String ti=dateString.replace(" ","");
        String ti2=ti.replace(":","");
        String time=ti2.replace("-","");
        long a= Long.valueOf(time).longValue();
        String t2=time.substring(0,4);
        Calendar calendarB=teacherAwardsMapper.selectCalendar(t2,"0");//第一学期0
        String t4=formatter.format(calendarB.getBeginTime());//线数据库时间2009-02-22 16:00:00
        String t5=t4.replace("-","");//20090222 16:00:00
        String t6=t5.replace(" ","");//2009022216:00:00
        String t7=t6.replace(":","");//20090222160000
        long bb=Long.valueOf(t7).longValue();
        Calendar calendarE=teacherAwardsMapper.selectCalendar(t2,"1");//第2学期1
        String te4=formatter.format(calendarE.getBeginTime());//线数据库时间2009-02-22 16:00:00
        String te5=te4.replace("-","");//20090222 16:00:00
        String te6=te5.replace(" ","");//2009022216:00:00
        String te7=te6.replace(":","");//20090222160000
        long be=Long.valueOf(te7).longValue();
        if(a<bb){
            //上一年的第二学期
            System.out.println("测试a小于b");
            String schoolYear=String.valueOf(Long.valueOf(t2).longValue()-1);
            Calendar calendar2=teacherAwardsMapper.selectCalendar(schoolYear,"1");
            long SemesterId= calendar2.getSemesterId();
            String semesterId=Long.toString(SemesterId);
            teachingInformationDTO.setCurrentTime(semesterId);
            teachingInformationVO.setXnXq(schoolYear+"-"+t2+"学年第2学期000");
        }else {
            if(a>=bb&&a<=be){
                System.out.println("测试a大于等于b且小于等于c");
                Calendar calendar4=teacherAwardsMapper.selectCalendar(t2,"0");
                long SemesterId=calendar4.getSemesterId();
                String semesterId=Long.toString(SemesterId);
                System.out.println("测试aaaaa"+SemesterId);
                System.out.println("测试bbbbb"+t2+"1");
                System.out.println("测试ccccc"+calendar4.getSchoolYear());
                System.out.println("测试ddddd"+calendar4.getSemester());
                teachingInformationDTO.setCurrentTime(semesterId);
                System.out.println("测试eeeee"+teachingInformationDTO.getCurrentTime());
                long y=Long.valueOf(t2).longValue()+1;
                String schoolYear2=Long.toString(y);
                teachingInformationVO.setXnXq(t2+"-"+schoolYear2+"学年第1学期");
            }
            if(a>be){
                System.out.println("测试a大于c");
                long y=Long.valueOf(t2).longValue()+1;
                String ye=Long.toString(y);
                Calendar calendar5=teacherAwardsMapper.selectCalendar(ye,"0");
                long SemesterId=calendar5.getSemesterId();
                String semesterId=Long.toString(SemesterId);
                teachingInformationDTO.setCurrentTime(semesterId);
                teachingInformationVO.setXnXq(t2+"-"+ye+"学年第2学期");
            }
        }
        System.out.println("测试fffff"+teachingInformationDTO.getCurrentTime());
        System.out.println("测试ggggg"+teachingInformationDTO.getTeacherGh());
        List<Timetable> timetableList=teacherTimeTableMapper.getTeachingTimeInformation(teachingInformationDTO.getTeacherGh(),teachingInformationDTO.getCurrentTime());
        long max=timetableList.size();
        List<Timetable> timetableList2=timetableList;
        long studentNum=0;
        double courseHours=0;
        if(timetableList!=null){
            for (int i = 0; i <timetableList.size() ; i++) {
                for  ( int  j  =  timetableList.size()  -   1 ; j  >  i; j -- )  {
                    if  (timetableList.get(j).getClassesId().equals(timetableList.get(i).getClassesId())){
                        timetableList.remove(j);
                        System.out.println("测试hhhhh"+timetableList);
                    }
                }
            }
            System.out.println("测试iiiii"+timetableList);
            if(timetableList!=null){
                List<Timetable> newTimeTableList=timetableList;//班
                for (int i = 0; i < newTimeTableList.size(); i++) {
                    String classesId=newTimeTableList.get(i).getClassesId();
                    Classes classes=teacherTimeTableMapper.getClassesName(classesId);//获取班级名
                    List classesList=new ArrayList<>();
                    if(classes!=null){
                        classesList.add(classes.getClassName());
                        teachingInformationVO.setClassesList(classesList);
                        long StuNum=teacherTimeTableMapper.getStudentNum(classes.getClassCode(),"1");//获取此班级人数
                        studentNum=studentNum+StuNum;
                    }else{
                        classesList.add("——");
                        teachingInformationVO.setClassesList(classesList);
                    }
                }
            }

            for (int m = 0; m <timetableList2.size() ; m++) {
                for  ( int  n  =  timetableList2.size()  -   1 ; n  >  m; n -- )  {
                    if  (timetableList2.get(n).getCourseId().equals(timetableList2.get(m).getCourseId())){
                        timetableList2.remove(n);
                        System.out.println("测试hhhhh"+timetableList2);
                    }
                }
            }
            System.out.println("测试iiiii"+timetableList2);
            long min=timetableList2.size();
            teachingInformationVO.setCourseNum(timetableList2.size());
            List<Timetable> newTimeTableList2=timetableList2;//课程
            System.out.println("测试kkkkk"+newTimeTableList2);
           if(timetableList2!=null){
               for (int i = 0; i < newTimeTableList2.size(); i++) {
                   String courseId=newTimeTableList2.get(i).getCourseId();
                   Course course=teacherTimeTableMapper.getCourseName(courseId);//获取课程名
//                   System.out.println("测试jjjjj"+course.getCourseName());
                   List courseList=new ArrayList<>();
                   System.out.println("测试lllll"+course!=null);
                   if(course!=null){
                       courseList.add(course.getCourseName());
                       courseHours=courseHours+course.getCourseHours();
                   }else if(course==null){
                       courseList.add("——");
                   }
                   teachingInformationVO.setCourseList(courseList);
               }
                   teachingInformationVO.setClassHour(courseHours*(max-min));
           }
        }
        teachingInformationVO.setStudentNum(studentNum);
        return R.Success("授课信息查询成功！",teachingInformationVO);
    }
}
